#include "textmenu.h"
#include "delay.h"
#include "led.h"
#include "key.h"
#include "stdio.h"

u8g2_t u8g2;
textMenu_t textMenu;

static void blink(void);
static void ledoff(void);
static void ledon(void);
static void backLightBrightness(void);

const char *menuItems[MAX_MENU] = {
    "1.Blink",
    "2.LED OFF",
    "3.LED ON",
    "4.Brightness",
    "5.Test",
};

void (*pSelectFuncCB[MAX_MENU])(void) = {
    blink,
    ledoff,
    ledon,
    backLightBrightness,
};

static unsigned char textMenu_KeyDownCheck(void)
{
    return Key_Scan(KEY1_GPIO_PORT, KEY1_GPIO_PIN);
}

static unsigned char textMenu_KeyDownCallback(void)
{
    printf("%s", __FUNCTION__);
    textMenu.curMenuID++;
    if (textMenu.curMenuID >= (MAX_MENU - 1)) {
        textMenu.curMenuID = (MAX_MENU - 1);
    }

    return 1;
}

static unsigned char textMenu_KeyUpCheck(void)
{
    return 0;
}

static unsigned char textMenu_KeyUpCallback(void)
{
    textMenu.curMenuID--;
    if (textMenu.curMenuID < 0) {
        textMenu.curMenuID = 0;
    }

    return 1;
}

static unsigned char textMenu_KeySelectCheck(void)
{
    return 0;
}

static void blink(void)
{
    while (textMenu.KeySelectCheck()) {
        LED1_TOGGLE;
        delay_ms(500);
    }
    delay_ms(1000);
}

static void ledoff(void)
{
    LED1_OFF;
    delay_ms(500);
}

static void ledon(void)
{
    LED1_ON;
    delay_ms(500);
}

static void backLightBrightness(void)
{
    unsigned char brightness = 100;
    char strOut[50] = {0};

    u8g2_ClearBuffer(&u8g2);
    while (textMenu.KeySelectCheck()) {
        if (textMenu.KeyDownCheck()) {
            brightness -= 5;
            delay_ms(200);
        }

        if (textMenu.KeyUpCheck()) {
            brightness += 5;
            delay_ms(200);
        }

        sprintf(strOut, "Brightness: %d", brightness);
        u8g2_DrawStr(&u8g2, 5, 25, (const char *)strOut);
        u8g2_SendBuffer(&u8g2);
        u8g2_ClearBuffer(&u8g2);
    }
    delay_ms(1000);
}

static void textMenu_DisplayInit(pTextMenu_t pTextMenu)
{
    unsigned char index = 0;

    u8g2_SetFont(&u8g2,
                 u8g2_font_ncenB08_tr);              // u8g2_font_ncenB08_tr font, should keep 11 pixels on y axis

    for (index = 0; index < MENUS_IN_ONEPAGE; index++) {
        u8g2_DrawStr(&u8g2, TEXT_WIDTH_OFFSET, TEXT_HEIGHT * index + 11, pTextMenu->pTmenuList[index]);
        //        u8g2_SendBuffer(&u8g2);
    }
    u8g2_DrawRFrame(&u8g2, 0, 0, LCD_WIDTH, BOX_HEIGHT, 2);
    u8g2_SendBuffer(&u8g2);
}

void textMenu_Init(pTextMenu_t pTextMenu)
{
    unsigned char menuItem_index = 0;

    for (menuItem_index = 0; menuItem_index < MAX_MENU; menuItem_index++) {
        pTextMenu->pTmenuList[menuItem_index] = menuItems[menuItem_index];
    }

    pTextMenu->textMenuSize      = (sizeof(menuItems) / sizeof(menuItems[0]));
    pTextMenu->curMenuID         = 0;
    pTextMenu->KeyDownCheck      = textMenu_KeyDownCheck;
    pTextMenu->KeyDownCallback   = textMenu_KeyDownCallback;
    pTextMenu->KeyUpCheck        = textMenu_KeyUpCheck;
    pTextMenu->KeyUpCallback     = textMenu_KeyUpCallback;
    pTextMenu->KeySelectCheck    = textMenu_KeySelectCheck;

    for (menuItem_index = 0; menuItem_index < MAX_MENU; menuItem_index++) {
        pTextMenu->KeySelectCallback[menuItem_index] = pSelectFuncCB[menuItem_index];
    }

    textMenu_DisplayInit(pTextMenu);
}


void textMenu_Handle(pTextMenu_t pTextMenu)
{
    unsigned char tMenu_index = 0;
    char strOut[50] = {0};

    if (pTextMenu->KeyDownCheck()) {
        pTextMenu->KeyDownCallback();
    }

    if (pTextMenu->KeyUpCheck()) {
        pTextMenu->KeyUpCallback();
    }

    if (pTextMenu->KeySelectCheck()) {
        pTextMenu->KeySelectCallback[pTextMenu->curMenuID]();
    }

    for (tMenu_index = 0; tMenu_index <= MAX_MENU; tMenu_index++) {
        if (textMenu.curMenuID < MENUS_IN_ONEPAGE) {
            if (textMenu.curMenuID == tMenu_index) {
                u8g2_DrawRFrame(&u8g2, 0, tMenu_index * TEXT_HEIGHT, LCD_WIDTH, BOX_HEIGHT, 2);
                u8g2_SetFont(&u8g2, u8g2_font_ncenB08_tr);
                u8g2_DrawStr(&u8g2, TEXT_WIDTH_OFFSET, TEXT_HEIGHT * tMenu_index + 11, pTextMenu->pTmenuList[tMenu_index]);
            }
        } else {
            if (textMenu.curMenuID == tMenu_index) {
                /* make sure new page menu text alway show at the last */
                u8g2_DrawRFrame(&u8g2, 0, (tMenu_index - (1 + textMenu.curMenuID - MENUS_IN_ONEPAGE)) * TEXT_HEIGHT, LCD_WIDTH,
                                BOX_HEIGHT, 2);
            }
            u8g2_DrawStr(&u8g2, TEXT_WIDTH_OFFSET, (tMenu_index - (1 + textMenu.curMenuID - MENUS_IN_ONEPAGE)) * TEXT_HEIGHT + 11,
                         pTextMenu->pTmenuList[tMenu_index]);
        }
    }

    u8g2_SendBuffer(&u8g2);
    //    u8g2_ClearBuffer(&u8g2);
}


